package com.dobby.pea.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dobby.pea.common.R;
import com.dobby.pea.dto.TypeDto;
import com.dobby.pea.entity.tbGood;
import com.dobby.pea.entity.tbType;
import com.dobby.pea.entity.tbTypeSecond;
import com.dobby.pea.mapper.TypeMapper;
import com.dobby.pea.service.GoodService;
import com.dobby.pea.service.TypeSecondService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dobby.pea.service.TypeService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Dobby
 * @version 1.0
 */
@Slf4j
@Service
public class TypeServiceImpl extends ServiceImpl<TypeMapper, tbType> implements TypeService {

//    @Autowired
//    private GoodService goodService;

    @Autowired
    private TypeSecondService typeSecondService;

    /**
     *删除一级目录，检查是否可以删除
     * @param ids
     */
    @Transactional
    public R<String> deleteFirst(List<Long> ids){
        LambdaQueryWrapper<tbTypeSecond> queryWrappertbGood = new LambdaQueryWrapper();
        queryWrappertbGood.in(tbTypeSecond::getTypeId,ids);

        int count = typeSecondService.count(queryWrappertbGood);

        if(count > 0){
            //如果不能删除，抛出异常
          return R.error("类别目录下有别的目录，不能删除");
        }

        //如果可以删除，则删除列别表中的数据
        this.removeByIds(ids);

        return R.success("已删除");
    }


    /**
     * 获取二级目录(带着一级目录）
     * @param list
     */
    @Transactional
    public List<TypeDto> typeWithSecond(List<tbType> list){

        List<TypeDto> typeDtos = list.stream().map((item) -> {
            LambdaQueryWrapper<tbTypeSecond> queryWrapper = new LambdaQueryWrapper();
            TypeDto typeDto = new TypeDto();
            BeanUtils.copyProperties(item,typeDto);
            //数据流 赋值二级目录
            queryWrapper.in(tbTypeSecond::getTypeId,item.getId());
            log.info("获取二级目录list-------------->>list:{}",item.getId());
            List<tbTypeSecond> typeSecond = typeSecondService.list(queryWrapper);
            log.info("获取二级目录(带着一级目录）-------------->>typeSecond:{}",typeSecond);


            typeDto.setImge(item.getImge());
            typeDto.setPriority(item.getPriority());
            typeDto.setTypename(item.getTypename());
            typeDto.setTbTypeSeconds(typeSecond);
            return typeDto;
        }).collect(Collectors.toList());

        return typeDtos;

    }

    /**
     * 保存二级目录
     * @param typeSecond
     * @return
     */
    @Transactional
    public R<String> saveSecond(tbTypeSecond typeSecond) {

        LambdaQueryWrapper<tbType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(tbType::getId,typeSecond.getTypeId());
        int count1 =this.count(queryWrapper);

        if (count1<=0) return R.error("没有该一级目录");

        LambdaQueryWrapper<tbTypeSecond> queryWrappersecond = new LambdaQueryWrapper<>();
        queryWrappersecond.in(tbTypeSecond::getName,typeSecond.getName());
        int count2 =typeSecondService.count(queryWrappersecond);

        if (count2>0) return R.error("已有该二级目录");

        //检查通过 保存数据库
        typeSecondService.save(typeSecond);

        return R.success("二级目录新建成功");
    }

    /**
     * 更新二级目录
     * @param tbTypeSecond
     */
    @Transactional
    public R<String> updateSecond(tbTypeSecond tbTypeSecond) {
        typeSecondService.updateById(tbTypeSecond);
        return null;
    }

    /**
     * 删除二级目录
     * @param ids
     */
    @Transactional
    public R<String> deleteSecond(List<Long> ids) {
//        LambdaQueryWrapper<tbGood> queryWrappertbGood = new LambdaQueryWrapper();
//        queryWrappertbGood.in(tbGood::getTypeId,ids);
//
//        int count = goodService.count(queryWrappertbGood);
//
//        if(count > 0){
//            //如果不能删除，抛出异常
//            return R.error("类别目录下有商品，不能删除");
//        }

        //如果可以删除，则删除列别表中的数据
        typeSecondService.removeByIds(ids);

        return R.success("已删除");
    }


}
